#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;
//find_if:按条件查找元素
//函数原型：find_if(iterator beg, iterator end, _Pred);  按值查找元素，找到返回指定位置迭代器，找不到返回结束迭代器位置
//beg 开始迭代器  end 结束迭代器  _Pred 函数或者谓词（返回bool类型的仿函数）

class Great {
public:
    bool operator()(int val) {
        return val > 50;
    }
};

void test1() {
    vector<int> v;
    for (int i = 0; i < 10; ++i) {
        v.push_back(i * i);
    }
    vector<int>::iterator it = find_if(v.begin(), v.end(), Great());
    if (it == v.end()) {
        cout << "没有找到" << endl;
    } else {
        cout << "找到了" << endl;
    }
}

class Person {
public:
    string name;
    int age;

    Person(string name, int age) {
        this->name = name;
        this->age = age;
    }

};

class GreatTo {
public:
    bool operator()(Person &p) {
        return p.age > 20;
    }
};

void test2() {

    vector<Person> v;

    //创建数据
    Person p1("aaa", 10);
    Person p2("bbb", 20);
    Person p3("ccc", 30);
    Person p4("ddd", 40);
    Person p5("eee", 50);

    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);

    vector<Person>::iterator it = find_if(v.begin(), v.end(), GreatTo());
    if (it == v.end()) {
        cout << "没有找到!" << endl;
    } else {
        cout << "找到姓名:" << it->name << " 年龄: " << it->age << endl;
    }
}

int main() {
    test1();
    test2();
    return 0;
}